package com.hw.ui.login;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.hw.HWApp;
import com.hw.base.model.BaseModel;
import com.hw.base.util.Util;
import com.hw.bean.AbLoginBean;
import com.hw.daobean.DataBaseControl;
import com.hw.daobean.RelationInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class LoginModelImpl extends BaseModel implements LoginModel {
    private String tag = "LoginModelImpl";
    private String msg = "";
    private AbLoginBean abLoginBean;

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        listener.onFail(msg);
                        break;
                    case 0:
                        listener.onloginSuccess();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void login(final String username, final String password, final LoginModel.CallBack listener) {
        Request request = new Request.Builder().url(
                HWApp.sSharedPreferences.getString(Util.addk, "") + Util.urlsuffix +
                        "?action=Login&cid=" +
                        HWApp.sSharedPreferences.getString(Util.cidk, "") +
                        "&userName=" + username +
                        "&password=" + password).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                msg = Util.netConnectionError;
                postHandle(listener, -1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {//回调的方法执行在子线程。
                    String result = response.body().string();
                    Log.e(tag, result);
                    try {
                        abLoginBean = gson.fromJson(result, AbLoginBean.class);
                        if (null != abLoginBean && abLoginBean.getErrcode() == 0) {
                            HWApp.sEditor.putInt(Util.userID, abLoginBean.getData().getUserID());
                            HWApp.sEditor.commit();
                            postHandle(listener, 0);
                        } else {
                            msg = abLoginBean.getErrmsg();
                            postHandle(listener, -1);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        msg = Util.exceptionError;
                        postHandle(listener, -1);
                    }
                } else {
                    msg = Util.netServerError;
                    postHandle(listener, -1);
                }
            }
        });
    }

    @Override
    public void checkDelete() {
        List<RelationInfo> list = new ArrayList<RelationInfo>();
        if (list.size() > 1500) {
            DataBaseControl.deleteRelationAll(list);
        }
    }
}

